package com.share51.config.mobile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration;
import org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAutoConfiguration;
import org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.view.MustacheViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

/**
 * @author gaobo
 * @version 1.0.0
 * @ClassName DeviceDelegatingViewResolverAutoConfiguration
 * @Description TODO
 * @Date 2020/11/2 10:03
 */
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass({LiteDeviceDelegatingViewResolver.class})
@ConditionalOnProperty(
        prefix = "spring.mobile.devicedelegatingviewresolver",
        name = {"enabled"},
        havingValue = "true"
)
@EnableConfigurationProperties({DeviceDelegatingViewResolverProperties.class})
@AutoConfigureAfter({WebMvcAutoConfiguration.class, FreeMarkerAutoConfiguration.class, GroovyTemplateAutoConfiguration.class, MustacheAutoConfiguration.class, ThymeleafAutoConfiguration.class})
public class DeviceDelegatingViewResolverAutoConfiguration {

    private static final Logger log = LoggerFactory.getLogger(DeviceDelegatingViewResolverAutoConfiguration.class);

    public DeviceDelegatingViewResolverAutoConfiguration() {
    }

    @Configuration
    @ConditionalOnClass({ThymeleafViewResolver.class})
    protected static class DeviceDelegatingThymeleafViewResolverConfiguration {

        protected DeviceDelegatingThymeleafViewResolverConfiguration() {
        }

        @Bean
        @ConditionalOnBean({ThymeleafViewResolver.class})
        public LiteDeviceDelegatingViewResolver deviceDelegatingThymeleafViewResolver(DeviceDelegatingViewResolverFactory factory, ThymeleafViewResolver viewResolver) {
            return factory.createViewResolver(viewResolver);
        }
    }

    @Configuration
    @ConditionalOnClass({MustacheViewResolver.class})
    protected static class DeviceDelegatingMustacheViewResolverConfiguration {
        protected DeviceDelegatingMustacheViewResolverConfiguration() {
        }

        @Bean
        @ConditionalOnBean({MustacheViewResolver.class})
        public LiteDeviceDelegatingViewResolver deviceDelegatingMustacheViewResolver(DeviceDelegatingViewResolverFactory factory, MustacheViewResolver viewResolver) {
            return factory.createViewResolver(viewResolver);
        }
    }

    @Configuration
    @ConditionalOnClass({InternalResourceViewResolver.class})
    protected static class DeviceDelegatingJspViewResolverConfiguration {
        protected DeviceDelegatingJspViewResolverConfiguration() {
        }

        @Bean
        @ConditionalOnBean({InternalResourceViewResolver.class})
        public LiteDeviceDelegatingViewResolver deviceDelegatingJspViewResolver(DeviceDelegatingViewResolverFactory factory, InternalResourceViewResolver viewResolver) {
            return factory.createViewResolver(viewResolver);
        }
    }

    @Configuration
    @ConditionalOnClass({GroovyMarkupViewResolver.class})
    protected static class DeviceDelegatingGroovyMarkupViewResolverConfiguration {
        protected DeviceDelegatingGroovyMarkupViewResolverConfiguration() {
        }

        @Bean
        @ConditionalOnBean({GroovyMarkupViewResolver.class})
        public LiteDeviceDelegatingViewResolver deviceDelegatingGroovyMarkupViewResolver(DeviceDelegatingViewResolverFactory factory, GroovyMarkupViewResolver viewResolver) {
            return factory.createViewResolver(viewResolver);
        }
    }

    @Configuration
    @ConditionalOnClass({FreeMarkerViewResolver.class})
    protected static class DeviceDelegatingFreeMarkerViewResolverConfiguration {
        protected DeviceDelegatingFreeMarkerViewResolverConfiguration() {
        }

        @Bean
        @ConditionalOnBean({FreeMarkerViewResolver.class})
        public LiteDeviceDelegatingViewResolver deviceDelegatingFreeMarkerViewResolver(DeviceDelegatingViewResolverFactory factory, FreeMarkerViewResolver viewResolver) {
            return factory.createViewResolver(viewResolver);
        }
    }

    @Configuration
    protected static class LiteDeviceDelegatingViewResolverFactoryConfiguration {
        protected LiteDeviceDelegatingViewResolverFactoryConfiguration() {
        }

        @Bean
        public DeviceDelegatingViewResolverFactory deviceDelegatingViewResolverFactory(DeviceDelegatingViewResolverProperties properties) {
            return new DeviceDelegatingViewResolverFactory(properties);
        }
    }
}
